{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np #导入numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3], dtype=np.float64)\n",
    "print(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4]\n"
     ]
    }
   ],
   "source": [
    "arr2 = np.array([0.2, 2.5, 4.8], dtype=\"i8\")\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(arr[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4 6 8]\n",
      "[2 4 6 8]\n"
     ]
    }
   ],
   "source": [
    "print(arr[slice(2,9,2)])\n",
    "print(arr[2:9:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "print(arr[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 4 5 6 7 8]\n"
     ]
    }
   ],
   "source": [
    "print(arr[2:9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.52217708 -0.86862171  1.17093737]\n",
      " [ 1.01926624 -0.32957166  0.16741657]]\n",
      "[[0.52217708 0.86862171 1.17093737]\n",
      " [1.01926624 0.32957166 0.16741657]]\n",
      "[[ 1. -0.  2.]\n",
      " [ 2. -0.  1.]]\n",
      "[[ 0. -1.  1.]\n",
      " [ 1. -1.  0.]]\n",
      "[[ 1. -1.  1.]\n",
      " [ 1. -0.  0.]]\n",
      "[[False False False]\n",
      " [False False False]]\n",
      "[[ 1.04435415 -1.73724343  2.34187474]\n",
      " [ 2.03853248 -0.65914331  0.33483314]]\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]]\n",
      "[[1 0 1]\n",
      " [1 0 1]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.random.randn(2, 3)\n",
    "print(arr1)\n",
    "print(np.abs(arr1))\n",
    "print(np.ceil(arr1))\n",
    "print(np.floor(arr1))\n",
    "print(np.rint(arr1))\n",
    "print(np.isnan(arr1))\n",
    "print(np.multiply(arr1, 2))\n",
    "print(np.divide(arr1, arr1))\n",
    "print(np.where(arr1 > 0, 1, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.03119057 -1.85733398  1.09271685]\n",
      " [ 0.42370709 -0.05623063 -0.1628675 ]]\n",
      "[[2.03119057 1.85733398 1.09271685]\n",
      " [0.42370709 0.05623063 0.1628675 ]]\n",
      "[[ 3. -1.  2.]\n",
      " [ 1. -0. -0.]]\n",
      "[[ 2. -2.  1.]\n",
      " [ 0. -1. -1.]]\n",
      "[[ 2. -2.  1.]\n",
      " [ 0. -0. -0.]]\n",
      "[[False False False]\n",
      " [False False False]]\n",
      "[[ 4.06238114 -3.71466796  2.18543371]\n",
      " [ 0.84741418 -0.11246127 -0.32573499]]\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]]\n",
      "[[1 0 1]\n",
      " [1 0 0]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.random.randn(2, 3)\n",
    "print(arr1)\n",
    "print(np.abs(arr1))\n",
    "print(np.ceil(arr1))\n",
    "print(np.floor(arr1))\n",
    "print(np.rint(arr1))\n",
    "print(np.isnan(arr1))\n",
    "print(np.multiply(arr1, 2))\n",
    "print(np.divide(arr1, arr1))\n",
    "print(np.where(arr1 > 0, 1, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 1 1]\n",
      " [2 2 4]]\n",
      "2.3333333333333335\n",
      "14\n",
      "4\n",
      "1\n",
      "1.247219128924647\n",
      "1.5555555555555556\n",
      "0\n",
      "1\n",
      "[ 4  5  6  8 10 14]\n",
      "**************************************************\n",
      "[[4 1 1]\n",
      " [2 2 4]]\n",
      "**************************************************\n",
      "[ 4  4  4  8 16 64]\n",
      "**************************************************\n",
      "[[ 4  4  4]\n",
      " [ 2  4 16]]\n",
      "********************\n",
      "[[4 1 1]\n",
      " [8 2 4]]\n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.random.randint(1, 5, (2, 3))\n",
    "print(arr1)\n",
    "print(np.mean(arr1))\n",
    "print(np.sum(arr1))\n",
    "print(np.max(arr1))\n",
    "print(np.min(arr1))\n",
    "print(np.std(arr1))\n",
    "print(np.var(arr1))\n",
    "print(np.argmax(arr1))\n",
    "print(np.argmin(arr1))\n",
    "print(np.cumsum(arr1))\n",
    "print(\"*\"*50)\n",
    "print(arr1)\n",
    "#(1,2,4)\n",
    "#(3,1,1)\n",
    "print(\"*\"*50)\n",
    "print(np.cumprod(arr1))\n",
    "print(\"*\"*50)\n",
    "print(np.cumprod(arr1, axis=1))\n",
    "print(\"*\"*20)\n",
    "print(np.cumprod(arr1, axis=0))\n",
    "print(\"*\"*5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#2.7.3比较函数\n",
    "arr1 = np.array([1, 2, 3, 4, 5])\n",
    "print(np.any(arr1 > 3))\n",
    "print(np.all(arr1 > 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7 6 0]\n",
      " [0 0 8]\n",
      " [8 3 4]]\n",
      "[[0 6 7]\n",
      " [0 0 8]\n",
      " [3 4 8]]\n",
      "[[0 0 7]\n",
      " [0 4 8]\n",
      " [3 6 8]]\n"
     ]
    }
   ],
   "source": [
    "#2.7.4排序函数\n",
    "arr1 = np.random.randint(0, 10, (3, 3))\n",
    "print(arr1)\n",
    "arr1.sort()\n",
    "print(arr1)\n",
    "arr1.sort(axis=0)\n",
    "print(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 1 0]\n",
      " [1 0 1]\n",
      " [3 2 2]]\n",
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "#2.7.5去重函数a\n",
    "rr1 = np.random.randint(0, 5, (3, 3))\n",
    "print(arr1)\n",
    "print(np.unique(arr1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 8 10 12]\n",
      " [14 16 18]]\n",
      "[[-6 -6 -6]\n",
      " [-6 -6 -6]]\n",
      "[[ 7 16 27]\n",
      " [40 55 72]]\n",
      "[[0.14285714 0.25       0.33333333]\n",
      " [0.4        0.45454545 0.5       ]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "arr2 = np.array([[7, 8, 9], [10, 11, 12]])\n",
    "print(arr1 + arr2)\n",
    "print(arr1 - arr2)\n",
    "print(arr1 * arr2)\n",
    "print(arr1 / arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[6 5]\n",
      " [4 3]\n",
      " [2 1]]\n",
      "(2, 3) (3, 2)\n",
      "**************************************************\n",
      "[[20 14]\n",
      " [56 41]]\n",
      "[[20 14]\n",
      " [56 41]]\n",
      "[[20 14]\n",
      " [56 41]]\n",
      "[28 73]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "arr2 = np.array([[6, 5], [4, 3], [2, 1]])\n",
    "#对于矩阵乘法来说，要求第一个矩阵的列数等于第二个矩阵的行数\n",
    "print(arr1)\n",
    "print(arr2)\n",
    "print(arr1.shape, arr2.shape)\n",
    "print(\"*\"*50)\n",
    "print(np.dot(arr1, arr2))\n",
    "print(arr1.dot(arr2))\n",
    "print(arr1 @ arr2)\n",
    "# 一个二维数组跟一个大小合适的一维数组的矩阵点积运算之后将会得到一个一维数组\n",
    "arr3 = np.array([6, 5, 4])\n",
    "print(arr1 @ arr3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
